package com.lemon.cloud.mp.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.comm.constants.CacheConstants;
import com.lemon.cloud.comm.constants.enums.LogBusType;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.log.annotation.SysLog;
import com.lemon.cloud.mp.entity.WxAccount;
import com.lemon.cloud.mp.service.WxAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("wxaccount")
@RestController
public class WxAccountController {

    @Autowired
    private WxAccountService wxAccountService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询
     * @param page
     * @param account
     * @return
     */
    @GetMapping("/page")
    public ResultMsg<IPage<WxAccount>> page(Page page, WxAccount account) {
        return ResultMsg.resultSuccess(wxAccountService.page(page, Wrappers.query(account)));
    }

    /**
     * 通过id查询公众号账户
     * @param id id
     * @return ResultMsg
     */
    @GetMapping("/{id}")
    public ResultMsg<WxAccount> getById(@PathVariable("id") Integer id) {
        return ResultMsg.resultSuccess(wxAccountService.getById(id));
    }

    /**
     * 新增公众号账户
     * @param wxAccount 公众号账户
     * @return ResultMsg
     */
    @SysLog(value = "新增公众号账户",businessType = LogBusType.ADD)
    @PostMapping
    @PreAuthorize("hasAuthority('mp_wxaccount_add')")
    public ResultMsg save(@RequestBody WxAccount wxAccount) {
        wxAccountService.save(wxAccount);
        redisTemplate.convertAndSend(CacheConstants.MP_REDIS_RELOAD_TOPIC, "重新加载公众号配置");
        return ResultMsg.resultSuccess();
    }

    /**
     * 修改公众号账户
     * @param wxAccount 公众号账户
     * @return ResultMsg
     */
    @SysLog(value ="修改公众号账户",businessType = LogBusType.MODIFY)
    @PutMapping
    @PreAuthorize("hasAuthority('mp_wxaccount_edit')")
    public ResultMsg updateById(@RequestBody WxAccount wxAccount) {
        wxAccountService.updateById(wxAccount);
        redisTemplate.convertAndSend(CacheConstants.MP_REDIS_RELOAD_TOPIC, "重新加载公众号配置");
        return ResultMsg.resultSuccess();
    }

    /**
     * 通过id删除公众号账户
     * @param id id
     * @return ResultMsg
     */
    @SysLog(value = "删除公众号账户",businessType = LogBusType.DELETE)
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('mp_wxaccount_del')")
    public ResultMsg removeById(@PathVariable("id") Integer id) {
        wxAccountService.removeById(id);
        redisTemplate.convertAndSend(CacheConstants.MP_REDIS_RELOAD_TOPIC, "重新加载公众号配置");
        return ResultMsg.resultSuccess();
    }

    /**
     * 生成公众号二维码
     * @param appId
     * @return
     */
    @SysLog(value = "生成公众号二维码",businessType = LogBusType.ADD)
    @PostMapping("/qr/{appId}")
    @PreAuthorize("hasAuthority('mp_wxaccount_add')")
    public ResultMsg qr(@PathVariable("appId") String appId) {
        return wxAccountService.generateQr(appId);
    }

    /**
     * 获取公众号列表
     * @return
     */
    @GetMapping("/list")
    public ResultMsg list() {
        return ResultMsg.resultSuccess(wxAccountService.list());
    }

    /**
     * 获取公众号接口数据
     * @param appId 公众号
     * @param interval 时间间隔
     * @return
     */
    @GetMapping("/statistics")
    public ResultMsg<List<List<Object>>> statistics(String appId, String interval) {
        return wxAccountService.statistics(appId, interval);
    }

}
